package spring.core.examples.validator.xml;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

/**
 * Demonstrates how to use validation in Spring according to JSR-303. Validation
 * meta information is provided by adding special Annotations to the beans.
 */
public class ApplicationJSR {
	/**
	 * Gets a bean from the spring application context
	 * 
	 * @param beanName
	 *            the bean name
	 * @return the bean
	 */
	private static Object getBean(String beanName) {
		return ValidatorContext.getInstance().getBean(beanName);
	}

	/**
	 * The main method.
	 * 
	 * @param args
	 *            the arguments
	 */
	public static void main(String[] args) {

		// Retrieve a person from Spring context that is using annotated
		// validation
		PersonWithValidation myPerson = (PersonWithValidation) getBean("personWithValidation");
		Validator myValidator = (LocalValidatorFactoryBean) getBean("validator");
		Set<ConstraintViolation<PersonWithValidation>> myResult = myValidator
				.validate(myPerson);

		// print out the errors found in all translations using the standard
		// message text.
		for (ConstraintViolation<PersonWithValidation> eachError : myResult) {
			System.out.println(eachError.getMessage());
		}

	}
}
